package com.ruoyi.ejkgl.ejkdb.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ejkgl.ejkdb.domain.Ejkdb;
import com.ruoyi.ejkgl.ejkdb.domain.EjkdbDbmx;
import com.ruoyi.ejkgl.ejkdb.mapper.EjkdbMapper;
import com.ruoyi.ejkgl.ejkdb.service.IEjkdbService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 二级库调拨Service业务层处理
 *
 * @author huacai
 * @date 2024-12-10
 */
@Service
public class EjkdbServiceImpl implements IEjkdbService {
    @Resource
    private EjkdbMapper ejkdbMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询二级库调拨
     *
     * @param ejkdbId 二级库调拨主键
     * @return 二级库调拨
     */
    @Override
    public Ejkdb selectEjkdbByEjkdbId(String ejkdbId) {
        return ejkdbMapper.selectEjkdbByEjkdbId(ejkdbId);
    }

    /**
     * 查询二级库调拨列表
     *
     * @param ejkdb 二级库调拨
     * @return 二级库调拨
     */
    @Override
    public List<Ejkdb> selectEjkdbList(Ejkdb ejkdb) {
        return ejkdbMapper.selectEjkdbList(ejkdb);
    }

    /**
     * 新增二级库调拨
     *
     * @param ejkdb 二级库调拨
     * @return 结果
     */
    @Transactional
    @Override
    public int insertEjkdb(Ejkdb ejkdb) {
        ejkdb.setCreateTime(DateUtils.getNowDate());
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ejkdb.setUserId(Math.toIntExact(loginUser.getUserId())); //用户ID
        ejkdb.setDeptId(Math.toIntExact(loginUser.getDeptId())); //部门ID
        ejkdb.setCreateBy(loginUser.getUsername()); //创建人
        ejkdb.setIdWhere("存在");

        int rows = ejkdbMapper.insertEjkdb(ejkdb);
        insertEjkdbDbmx(ejkdb);
        return rows;
    }

    /**
     * 批量新增二级库调拨
     *
     * @param ejkdbs 二级库调拨List
     * @return 结果
     */
    @Override
    public int batchInsertEjkdb(List<Ejkdb> ejkdbs) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(ejkdbs)) {
            try {
                for (int i = 0; i < ejkdbs.size(); i++) {
                    int row = ejkdbMapper.insertEjkdb(ejkdbs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == ejkdbs.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改二级库调拨
     *
     * @param ejkdb 二级库调拨
     * @return 结果
     */
    @Transactional
    @Override
    public int updateEjkdb(Ejkdb ejkdb) {
        ejkdb.setUpdateTime(DateUtils.getNowDate());
        ejkdbMapper.deleteEjkdbDbmxByEjkdbId(ejkdb.getEjkdbId());
        insertEjkdbDbmx(ejkdb);
        return ejkdbMapper.updateEjkdb(ejkdb);
    }

    /**
     * 批量删除二级库调拨
     *
     * @param ejkdbIds 需要删除的二级库调拨主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEjkdbByEjkdbIds(String[] ejkdbIds) {
        ejkdbMapper.deleteEjkdbDbmxByEjkdbIds(ejkdbIds);
        return ejkdbMapper.deleteEjkdbByEjkdbIds(ejkdbIds);
    }

    /**
     * 删除二级库调拨信息
     *
     * @param ejkdbId 二级库调拨主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEjkdbByEjkdbId(String ejkdbId) {
        ejkdbMapper.deleteEjkdbDbmxByEjkdbId(ejkdbId);
        return ejkdbMapper.deleteEjkdbByEjkdbId(ejkdbId);
    }

    /**
     * 新增调拨明细信息
     *
     * @param ejkdb 二级库调拨对象
     */
    public void insertEjkdbDbmx(Ejkdb ejkdb) {
        List<EjkdbDbmx> ejkdbDbmxList = ejkdb.getEjkdbDbmxList();
        String ejkdbId = ejkdb.getEjkdbId();
        if (StringUtils.isNotNull(ejkdbDbmxList)) {
            List<EjkdbDbmx> list = new ArrayList<EjkdbDbmx>();
            for (EjkdbDbmx ejkdbDbmx : ejkdbDbmxList) {
                ejkdbDbmx.setEjkdbId(ejkdbId);
                list.add(ejkdbDbmx);
            }
            if (list.size() > 0) {
                ejkdbMapper.batchEjkdbDbmx(list);
            }
        }
    }
}
